﻿using System;
using ArtMath.Parameter;
using System.ComponentModel;
using System.Drawing.Design;
using System.Windows.Forms.Design;
using ArtMath.Helper;
using ArtMath.Control;
using ArtMath.Core.Helper;

namespace ArtMath.Editor
{
    [Serializable]
    public class SketchpadEditor : UITypeEditor
    {
        public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
        {
            if (context?.Instance != null)
            {
                return UITypeEditorEditStyle.DropDown;
            }
            return base.GetEditStyle(context);
        }
        public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
        {
            if (context?.Instance != null && provider != null)
            {
                IWindowsFormsEditorService es = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
                if (es != null)
                {
                    ObjectSelector<SketchPad> editor;
                    Array arr = context.Instance as Array;
                    BasicParameter para = null;
                    if (arr != null)
                    {
                        string[] val = new string[arr.Length];
                        for (int i = 0; i < val.Length; i++)
                        {
                            if (para == null)
                            {
                                para = arr.GetValue(i) as BasicParameter;
                            }
                            val[i] = (arr.GetValue(i) as BasicParameter).ShowInPads.Trim(';', '；');
                        }
                        editor = new ObjectSelector<SketchPad>(para.Document.Pads, val.Same() ? val[0].Split(';', '；') : new string[0]);
                    }
                    else
                    {
                        para = context.Instance as BasicParameter;
                        editor = new ObjectSelector<SketchPad>(para.Document.Pads, para.ShowInPads.Split(';', '；'));
                        es.DropDownControl(editor);
                        para.ShowInPads = editor.NewSelectedObjectNames;
                        return para.ShowInPads;
                    }
                }
            }
            return value;
        }
    }
}
